﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class ServiceDivisionsDataAccess : DivisionsDataAccess
    {
        public ServiceDivisionsDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<ServiceDivision> SelectServiceDivisionsList()
        {
            try
            {
                //Select Division List
                List<Division> divList = SelectDivisionsList();

                //Select Organizational Divisions List
                List<SERVICE_DIVISION> orgDivList = dataContext.SERVICE_DIVISION.ToList();

                //Merge result
                var res = (from div in divList
                           join orgDiv in orgDivList on div.ID equals orgDiv.ID
                           select new ServiceDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = div.CLASS_ID,
                               EXT_ID = div.EXT_ID,
                               ID = div.ID,
                               NAME = div.NAME,
                               PARENT_ID = div.PARENT_ID,
                               BOSS_ID = div.BOSS_ID,
                               DATE_CREATED = div.DATE_CREATED,
                               DESCRIPTION = div.DESCRIPTION,
                               ROOM_ID = orgDiv.ROOM_ID,
                               TELEPHONE = orgDiv.TELEPHONE
                           }).ToList();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectServiceDivisionBy_Id");
            }
        }

        public ServiceDivision SelectServiceDivisionBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                Division divisionPart = SelectDivisionBy_Id(Id);

                //Select Division part
                List<SERVICE_DIVISION> orgDivisionPart = dataContext.SERVICE_DIVISION.ToList();

                //Merge result
                var res = (from orgDiv in orgDivisionPart
                           where orgDiv.ID == divisionPart.ID
                           select new ServiceDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = divisionPart.CLASS_ID,
                               EXT_ID = divisionPart.EXT_ID,
                               ID = divisionPart.ID,
                               NAME = divisionPart.NAME,
                               PARENT_ID = divisionPart.PARENT_ID,
                               BOSS_ID = divisionPart.BOSS_ID,
                               DATE_CREATED = divisionPart.DATE_CREATED,
                               DESCRIPTION = divisionPart.DESCRIPTION,
                               ROOM_ID = orgDiv.ROOM_ID,
                               TELEPHONE = orgDiv.TELEPHONE
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectServiceDivisionBy_Id");
            }
        }

        public void InsertServiceDivision(ServiceDivision serviceDivision, ref int generated_Id)
        {
            try
            {
                //Insert the Division part
                InsertDivision(serviceDivision, ref generated_Id);

                //Insert the serviceDivision part
                int new_Id = generated_Id;
                SERVICE_DIVISION serviceDivisionPart = new SERVICE_DIVISION();
                serviceDivisionPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                serviceDivisionPart.ID = new_Id;
                serviceDivisionPart.ROOM_ID = serviceDivision.ROOM_ID;
                serviceDivisionPart.TELEPHONE = serviceDivision.TELEPHONE;

                //Submit changes
                dataContext.AddToSERVICE_DIVISION(serviceDivisionPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertServiceDivision");
            }
        }

        public void UpdateServiceDivision(ServiceDivision serviceDivision)
        {
            try
            {
                //Update the Division part
                UpdateDivision(serviceDivision);

                //Update the serviceDivision part
                SERVICE_DIVISION serviceDivisionPart = dataContext.SERVICE_DIVISION.Where(t => t.ID == serviceDivision.ID).FirstOrDefault();
                serviceDivisionPart.ROOM_ID = serviceDivision.ROOM_ID;
                serviceDivisionPart.TELEPHONE = serviceDivision.TELEPHONE;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateServiceDivision");
            }
        }

        public void DeleteServiceDivision(ServiceDivision serviceDivision)
        {
            try
            {
                //Delete the serviceDivision part
                SERVICE_DIVISION divPart = dataContext.SERVICE_DIVISION.Where(t => t.ID == serviceDivision.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();

                //Delete the Division part
                DeleteDivision(serviceDivision);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteServiceDivision");
            }
        }
    }
}
